local tuntian = fk.CreateSkill {

  name = "joy__tuntian",

  tags = {  },

}



tuntian:addEffect(fk.AfterCardsMove, {
  name = "joy__tuntian",
  anim_type = "special",
  derived_piles = "dengai_field",
  events = {fk.AfterCardsMove,fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
    if player:hasSkill(tuntian.name) and player.phase == Player.NotActive then
      for _, move in ipairs(data) do
        if move.from==player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
    elseif event == fk.TurnEnd then
      return target == player and player:hasSkill(tuntian.name) and not player:isKongcheng()
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      local card = player.room:askForDiscard(player,1,1,false,tuntian.name,true,nil,"#joy__tuntian",true)
      if #card > 0 then
        event:setCostData(self, card)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      room:throwCard(event:getCostData(self), tuntian.name, player, player)
    end
    if player.dead then return end
    local judge = {
      who = player,
      reason = tuntian.name,
      pattern = ".|.|spade,club,diamond",
    }
    room:judge(judge)
  end,
})
tuntian:addEffect(fk.TurnEnd, {
  name = "joy__tuntian",
  anim_type = "special",
  derived_piles = "dengai_field",
  events = {fk.AfterCardsMove,fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
    if player:hasSkill(tuntian.name) and player.phase == Player.NotActive then
      for _, move in ipairs(data) do
        if move.from==player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
    elseif event == fk.TurnEnd then
      return target == player and player:hasSkill(tuntian.name) and not player:isKongcheng()
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnEnd then
      local card = player.room:askForDiscard(player,1,1,false,tuntian.name,true,nil,"#joy__tuntian",true)
      if #card > 0 then
        event:setCostData(self, card)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      room:throwCard(event:getCostData(self), tuntian.name, player, player)
    end
    if player.dead then return end
    local judge = {
      who = player,
      reason = tuntian.name,
      pattern = ".|.|spade,club,diamond",
    }
    room:judge(judge)
  end,
})
tuntian:addEffect(fk.FinishJudge, {
  refresh_events = {fk.FinishJudge},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(tuntian.name) and data.reason == tuntian.name
  end,
  on_refresh = function(self, event, target, player, data)
    if data.card.suit ~= Card.Heart and player.room:getCardArea(data.card) == Card.Processing then
      player:addToPile("dengai_field", data.card, true, tuntian.name)
    end
  end,
})

tuntian:addEffect("distance", {
  name = "#joy__tuntian_distance",
  correct_func = function(self, from, to)
    if from:hasSkill(tuntian.name) then
      return -#from:getPile("dengai_field")
    end
  end,
})

return tuntian